﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
//
public class Example2 : MonoBehaviour
{

    public string cubeRef;
    public string npcRef;

    List<AsyncOperationHandle> cube_Handles = new List<AsyncOperationHandle>();
    List<AsyncOperationHandle> npc_Handles = new List<AsyncOperationHandle>();
    private void OnGUI()
    {
        if (GUILayout.Button("InstantiateAsync"))
        {
            for(int i = 0; i < 8; i++)
            {
                var handle = Addressables.InstantiateAsync(cubeRef);
                handle.Completed += e =>
                {
                    Debug.LogError("实例化："+i+" " + e.Result.name);
                };
                cube_Handles.Add(handle);
            }
            for (int i = 0; i < 6; i++)
            {
                var handle = Addressables.InstantiateAsync(npcRef);
                handle.Completed += e =>
                {
                    Debug.LogError("实例化：" + i + " " + e.Result.name);
                };
                npc_Handles.Add(handle);
            }

        }
        if (GUILayout.Button("ReleaseInstance"))
        {
            foreach(var value in cube_Handles)
            {
                Addressables.ReleaseInstance(value);
            }

            foreach (var value in npc_Handles)
            {
                Addressables.ReleaseInstance(value);
            }
        }

        
        
    }

}
